<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.qiqianyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use app\admin\model\RouteModel;
use app\company\model\MemberGroupModel;
use app\common\model\DistrictModel;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\ThemeModel;


class AdminMemberGroupController extends AdminBaseController
{
    /**
     * 文章分类列表
     * @adminMenu(
     *     'name'   => '分类管理',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章分类列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {

        $memberGroupModel = new MemberGroupModel();



        $param = $this->request->param();
        $where = [
            'delete_time'    => 0,
        ];


        if(!empty($param['post']['province_id'])){
            $where['province_id'] =  $param['post']['province_id'];
        }

        if(!empty($param['post']['city_id'])){
            $where['city_id'] =  $param['post']['city_id'];
        }

        if(!empty($param['post']['country_id'])){
            $where['country_id'] =  $param['post']['country_id'];
        }

        if(!empty($param['post']['district_id'])){
            $where['district_id'] =  $param['post']['district_id'];
        }

        if(!empty($param['post']['name'])){
            $where['name'] = ['like', "%".$param['post']['name']."%"];
        }




        $qsList        = $memberGroupModel->alias('a')->where($where) ->order('id', 'DESC') ->paginate(10);

        //echo $qaModel->getLastSql();
        $qsList->appends($param);

        $this->assign('qa_list', $qsList->items());
        $this->assign('page', $qsList->render());



        $defaultDistrict = empty($param['post']['district_id'])?"0":$param['post']['district_id'];
        if(empty($defaultDistrict)){

            $defaultDistrict = empty($param['post']['country_id'])?"0":$param['post']['country_id'];
        }

        if(empty($defaultDistrict)){

            $defaultDistrict = empty($param['post']['city_id'])?"0":$param['post']['city_id'];
        }

        if(empty($defaultDistrict)){
            $defaultDistrict = empty($param['post']['province_id'])?"0":$param['post']['province_id'];

        }
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);
        $this->assign("default_district", $defaultDistrict);


        return $this->fetch();
    }

    /**
     * 添加文章分类
     * @adminMenu(
     *     'name'   => '添加文章分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章分类',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $defaultDistrict = 0;
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);
        $this->assign("default_district", $defaultDistrict);

        return $this->fetch();
    }

    /**
     * 添加文章分类提交
     * @adminMenu(
     *     'name'   => '添加文章分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章分类提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $data = $this->request->param();

        $result = $this->validate($data['post'], 'MemberGroup');

        if ($result !== true) {
            $this->error($result);
        }


        $data['post']["create_time"] =  time();
        $data['post']["update_time"] =  time();

        $data['post']["create_date"]  = strtotime( $data['post']["create_date"]);

        $model = new MemberGroupModel ();
        $model->allowField(true)->data($data['post'], true)->isUpdate(false)->save();

        if ($result === false) {
            $this->error('添加失败!');
        }

        $this->success('添加成功!', url('/company/admin_member_group/index'));

    }

    /**
     * 编辑文章分类
     * @adminMenu(
     *     'name'   => '编辑文章分类',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
            $id = $this->request->param('id', 0, 'intval');

            $qaModel = new MemberGroupModel();
            $post            = $qaModel->where('id', $id)->find();
            $this->assign('post', $post);



        $defaultDistrict = $post['district_id'];
        $districts = $this->getDistrict($defaultDistrict);
        $this->assign("districts", $districts);
        $this->assign("default_district", $defaultDistrict);



        return $this->fetch();

    }

    /**
     * 编辑文章分类提交
     * @adminMenu(
     *     'name'   => '编辑文章分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章分类提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

        $result = $this->validate($data['post'], 'MemberGroup');

        if ($result !== true) {
            $this->error($result);
        }

        $data['post']["update_time"] =  time();
        $data['post']["create_date"]  = strtotime( $data['post']["create_date"]);





        $model = new MemberGroupModel ();
        $model->allowField(true)->data($data['post'], true)->isUpdate(true)->save();

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!', url('/company/admin_member_group/index'));
    }

    public function delete()
    {
        $data = $this->request->param();
        $id = $data['id'];

        $data['delete_time']=time();

        $model = Db::name("member_group");

        $model->where('id', $id)->update($data);

        $this->success('删除成功!');
    }

    public function getDistrict($id)
    {
        $districts = array(
            "name" => "",
            "options" => array(
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => ""),
                array("id" => 0, "options" => "")
            )
        );
        $districtModel = new DistrictModel();
        $district = $districtModel->find($id);
        if (empty($district)) {
            $districts['options'][0]['options'] = $districtModel->where(array("parentid" => 0))->order("listorder")->select();;
        } else {
            if ($district['level'] < 4) {
                $districts['options'][$district['level']]["options"] = $districtModel->where(array("parentid" => $district['id']))->order("listorder")->select();
            }
            while ($district['level'] >= 1) {
                $districts['options'][$district['level'] - 1]["id"] = $district['id'];
                $districts['options'][$district['level'] - 1]["options"] = $districtModel->where(array("parentid" => $district['parentid']))->order("listorder")->select();
                $districts['name'] = $district['name'] . $districts['name'];
                $district = $districtModel->find($district['parentid']);
            }
        }
        return $districts;
    }


    public function district($parentid = 0, $type = "array")
    {
        $districtModel = new DistrictModel();
        $district = $districtModel->where(array("parentid" => $parentid))->order("listorder")->select();
        if ($type == "array") {
            return $district;
        } else {
            $this->result($district);
        }
    }



}
